package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.TimeSlot;
import com.woniu.mapper.TimeSlotMapper;
import com.woniu.service.TimeSlotService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.vo.StaffVO;
import com.woniu.vo.TimeSlotVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class TimeSlotServiceImpl implements TimeSlotService {
    @Resource
    private TimeSlotMapper timeSlotMapper;
    @Override
    public ResponseUtil findByDoctorId(Integer doctorId) {
        List<TimeSlot> timeSlots = timeSlotMapper.findByDoctorId(doctorId);
        return !timeSlots.isEmpty()?ResponseUtil.get(ResponseEnum.OK,timeSlots)
                :ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil updateTimeSlot(Integer id) {
        TimeSlot timeSlot = timeSlotMapper.selectByPrimaryKey(id);
        if (timeSlot==null){
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
        if (timeSlot.getCurrentNum()>=timeSlot.getMaxNum()){
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
        timeSlot.setCurrentNum(timeSlot.getCurrentNum()+1);
        int i = timeSlotMapper.updateByPrimaryKeySelective(timeSlot);

        return i>0?ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TimeSlotVO> timeSlotVOList = timeSlotMapper.findAll();
        PageInfo pageInfo = new PageInfo(timeSlotVOList);
        return timeSlotVOList != null ?ResponseUtil.get(ResponseEnum.OK,pageInfo)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil deleteByPrimaryKey(Integer id) {
        TimeSlot timeSlot = timeSlotMapper.selectByPrimaryKey(id);
        if (timeSlot == null){
            return ResponseUtil.get(ResponseEnum.FAIL,"排班记录不存在");
        }
        if (timeSlot.getStatus() == 1){
            return ResponseUtil.get(ResponseEnum.FAIL,"在可挂号中，不能被删除");
        }
        int rows = timeSlotMapper.deleteByPrimaryKey(id);
        return rows == 1 ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil insert(TimeSlot record) {
        if (record.getStartTime() != null && record.getStartTime().before(new Date())){
            return ResponseUtil.get(ResponseEnum.FAIL,"开始时间不能早于当前时间");
        }
        if(record.getStartTime() != null && record.getEndTime() != null && record.getStartTime().after(record.getEndTime())){
            return ResponseUtil.get(ResponseEnum.FAIL,"开始时间必须早于结束时间");
        }
        if (record.getDoctorId() != null && record.getStartTime() != null && record.getEndTime() != null){
            List<TimeSlot> timeSlots = timeSlotMapper.findByDoctorTime(record.getDoctorId(), record.getStartTime(),record.getEndTime());
            if (timeSlots != null && !timeSlots.isEmpty()){
                return ResponseUtil.get(ResponseEnum.FAIL,"该医生在此时间段已有排班，不能重复排班");
            }
        }
        int rows = timeSlotMapper.insert(record);
        return rows == 1 ? ResponseUtil.get(ResponseEnum.OK)
                :ResponseUtil.get(ResponseEnum.FAIL);
    }
}
